export const datetimeToString = (obj, includeTime=false, daySep='-', timeSep=':', link=' ') => {
  if (!obj) obj = new Date()
  const ot = new Date(obj)
  const y = ot.getFullYear()
  const m = (ot.getMonth() + 1 + '').padStart(2, '0')
  const d = (ot.getDate() + '').padStart(2, '0')
  let ret = `${y}${daySep}${m}${daySep}${d}`
  if (includeTime) {
    const hh = (ot.getHours() + '').padStart(2, '0')
    const mm = (ot.getMinutes() + '').padStart(2, '0')
    const ss = (ot.getSeconds() + '').padStart(2, '0')
    ret = ret + `${link}${hh}${timeSep}${mm}${timeSep}${ss}`
  }
  return ret
}

// 计算日期的x天前
export const dateIntervalDays = (obj, days = 0) => {
  const ot = new Date(obj)
  const target = new Date(ot.getTime() + (days * 24 * 60 * 60 * 1000))
  const m = (target.getMonth() + 1 + '').padStart(2, '0')
  const d = (target.getDate() + '').padStart(2, '0')
  return `${target.getFullYear()}-${m}-${d}`
}

// 获取星期x
export const dateWeekName = (obj, t='short') => {
  if (!obj) obj = new Date()
  const ot = new Date(obj)
  const weekDays = ['日', '一', '二', '三', '四', '五', '六']
  let wname = weekDays[ot.getDay()]
  if (t === 'long') {
    wname = '星期' + wname
  }
  return wname
}

// 获取一年内的日期
export const dateInYear = (year = 2020, noweek = true) => {
  let obj = new Date(year, 0, 1)
  let dateList = []
  while (obj <= new Date(year, 11, 31)) {
    if ((obj.getDay() === 0 || obj.getDay()  === 6) && noweek) {
      //
    } else {
      const m = (obj.getMonth() + 1 + '').padStart(2, '0')
      const d = (obj.getDate() + '').padStart(2, '0')
      dateList.push(`${m}-${d}`)
    }
    obj = new Date(obj.getTime() + 24 * 60 * 60 * 1000)
  }
  return dateList
}
